package echat.future.cn.echat.my.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.hyphenate.chat.EMClient;

import echat.future.cn.echat.my.bean.Token;
import echat.future.cn.echat.my.bean.UserBean;
import echat.future.cn.echat.my.bean.WithdrawAccount;
import lumingweihua.future.cn.baselibgxh.base.MyApplication;
import lumingweihua.future.cn.baselibgxh.utils.SharedPreference;

/**
 * Created by 86936 on 2018/8/9.
 * 用户帮助类
 * @QQ 869360026
 */
public class UserHelper {

    public  static final String CONFIG = "E_CHAT_CONFIG";
    public static final String USER_CONFIG = "USER_CONFIG";
    public static final String TOKEN = "TOKEN";

    /**
     * 用户信息
     */
    private static UserBean userBean = null;

    /**
     * 初始化的类
     */
    private static  Context mContext;

    private static WithdrawAccount withdrawAccount = new WithdrawAccount();



    /**
     * 初始化工具类
     */
    public static void init(Context context){
        mContext = context;
    }


    /**
     * 获取用户信息
     * @return
     */
    public static UserBean getUser(){
        if(userBean == null){
            SharedPreferences sharedPreference = mContext.getSharedPreferences(CONFIG,0);
            String jsonString = sharedPreference.getString(USER_CONFIG,"");
            if("".equals(jsonString)){
//                //跳转到登陆页面失效页面
//                RouterHelper.toInvalidActivity(mContext);
                userBean = null;
            }else{
                userBean =  new Gson().fromJson(jsonString,UserBean.class);
            }
        }
        return userBean;
    }

    /**
     * 只能登陆时候调用
     * @param userBean
     */
    public static void save(UserBean userBean){
        if(userBean == null) {
            throw new RuntimeException("userBean is not null");
        }
        //更新本地引用
        UserHelper.userBean = userBean;
        mContext.getSharedPreferences(CONFIG,0).edit().putString(USER_CONFIG,new Gson().toJson(userBean)).apply();
    }

    public static String token;
    public static void  saveToken(Token toekn){
        UserHelper.token = toekn.getToken();
        SharedPreference.saveToSP(mContext,"token",toekn.getToken());
        SharedPreference.saveToSP(mContext,"uid",toekn.getMember_id());
        mContext.getSharedPreferences(CONFIG,0).edit().
                putString(TOKEN,new Gson().toJson(toekn))
                .apply();
    }


    public static String getToken(){
        try {
            if(token == null){
                token = new Gson().fromJson(mContext.getSharedPreferences(CONFIG,0).getString(TOKEN,null),Token.class).getToken();
            }
            SharedPreference.saveToSP(mContext,"token",token);
        } catch (Exception e) {
            // TODO: handle exception
        }

        return token;
    }

    public static String getUserId(){
       return new Gson().fromJson(mContext.getSharedPreferences(CONFIG,0).getString(TOKEN,null),Token.class).getMember_id();
    }

    public static void clean(){
        token = null;
        userBean = null;
        mContext.getSharedPreferences(CONFIG,0).edit().clear().apply();

        SharedPreference.saveToSP(mContext,"neteaseToken","");
        SharedPreference.saveToSP(mContext,"neteaseAccount","");
        MyApplication.getInstance().setNeteaseToken("");
        SharedPreference.saveToSP(mContext,"phone","");
        MyApplication.getInstance().setPhone("");
        EMClient.getInstance().logout(true);
    }



    public static void setAccount(WithdrawAccount account){
        if(withdrawAccount == null){
            withdrawAccount = new WithdrawAccount();
        }
        if(account == null){
            withdrawAccount.setAlipay_account("");
            withdrawAccount.setAlipay_username("");
            withdrawAccount.setMobile("");
        }else{
            withdrawAccount.setAlipay_account(account.getAlipay_account());
            withdrawAccount.setAlipay_username(account.getAlipay_username());
            withdrawAccount.setMobile(account.getMobile());
        }
    }
    public static WithdrawAccount getWithdrawAccount(){
        return withdrawAccount;
    }


    public static void cleanAccount(){
        withdrawAccount = null;
    }

}
